/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.huangp.framework;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Patrick Huang
 */
public class ThreadContext {
    //--------------------------------------------------------------------------
    // Static Properties
    //--------------------------------------------------------------------------
    private static final Logger logger = LoggerFactory.getLogger(ThreadContext.class);
    //--------------------------------------------------------------------------
    // Properties
    //--------------------------------------------------------------------------
    /**
     * threadLocal map which holds a map of values per thread
     */
    private static ThreadLocal<Map> threadLocalMap = new ThreadLocal<Map>(){
        @Override
        public Map<String,Serializable> initialValue(){
            return new HashMap<String,Serializable>();
        }
    };
    // -------------------------------------------------------------------------
    // Implementation Methods
    // -------------------------------------------------------------------------
    /**
     * return a thread local map in current thread
     * @return
     */
    public static Map<String,Serializable> getThreadLocalMap(){
        return threadLocalMap.get();
    }
    /**
     * reset threadLocal map
     */
    public static void resetMap(){
        threadLocalMap.get().clear();
    }

    /**
     * get a value from current threadLocal map
     * @param key
     * @return
     */
    public static Serializable getValue(String key){
        return (Serializable) threadLocalMap.get().get(key);
    }

    /**
     * put a value into current threadLocal map
     * @param key
     * @param value
     */
    public static void putValue(String key,Serializable value){
        threadLocalMap.get().put(key, value);
    }

    /**
     * remove a value from current threadLocal map
     * @param key
     */
    public static void removeValue(String key){
        threadLocalMap.get().remove(key);
    }

    /**
     * logger.info current threadLocal map values
     */
    public static void printCurrentThreadLocalMap(){
        logger.info("current threadLocal map:{}",threadLocalMap.get());
    }
    // -------------------------------------------------------------------------
    // Mutator Methods
    // -------------------------------------------------------------------------
}
